/**
 * 
 */
package com.axdoc.wallet.service.impl;

import java.sql.Timestamp;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.axdoc.common.constant.MqConstant;
import com.axdoc.common.constant.WalletConstant;
import com.axdoc.wallet.dao.IDoctorWalletDao;
import com.axdoc.wallet.dao.IMqRecordsDao;
import com.axdoc.wallet.dao.IPatientWalletDao;
import com.axdoc.wallet.dao.IPlatformWalletDao;
import com.axdoc.wallet.entity.DoctorBalanceSubWallet;
import com.axdoc.wallet.entity.DoctorBalanceSubWalletRecords;
import com.axdoc.wallet.entity.MqRecords;
import com.axdoc.wallet.entity.PatientActivitySubWalletRecords;
import com.axdoc.wallet.entity.PatientBalanceSubWallet;
import com.axdoc.wallet.entity.PatientBalanceSubWalletRecords;
import com.axdoc.wallet.entity.PatientCouponSubWalletRecords;
import com.axdoc.wallet.entity.PatientVipSubWalletRecords;
import com.axdoc.wallet.entity.PlatformWallet;
import com.axdoc.wallet.entity.PlatformWalletRecords;
import com.axdoc.wallet.exceptions.WalletBizException;
import com.axdoc.wallet.service.BizWalletService;
import com.axdoc.wallet.service.DoctorWalletService;
import com.axdoc.wallet.service.PatientWalletService;

/**
 * @author liyanhui
 *
 */
@Service("bizWalletService")
public class BizWalletServiceImpl implements BizWalletService {
	@Autowired
	private IPatientWalletDao patientWalletDao;
	@Autowired
	private IPlatformWalletDao platformWalletDao;
	@Autowired
	private IDoctorWalletDao doctorWalletDao;
	@Autowired
	private IMqRecordsDao mqRecordsDao;
	@Autowired
	private PatientWalletService patientWalletService;
	@Autowired
	private DoctorWalletService doctorWalletService;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordPaySuccess(String require, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(require)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数require");
		}
		JSONObject json = JSONObject.parseObject(require);
		JSONObject patient = json.getJSONObject("patient");
		JSONArray platform = json.getJSONArray("platform");
		String msgId = json.getString("msgId");
		long time = 0l;
		if(null != json.getLong("biz_time")){
			time = json.getLong("biz_time");
		}
		// 判断消息是否已经消费
		MqRecords mqRecords = mqRecordsDao.getMqRecord(Integer.valueOf(msgId));
		if (mqRecords != null) {
			int state = mqRecords.getState();
			if (state == 1) {
				throw new WalletBizException(WalletBizException.ERROR, "消息重复发送，丢弃");
			}
		}
		// patient内容
		String aid = patient.getString("aid");
		PatientBalanceSubWallet patientBalanceSubWallet = patientWalletDao.queryPatientBalanceSubWalletByAid(aid);
		if (patientBalanceSubWallet == null) {
			// 患者余额子账户不存在，创建账户
			patientWalletService.createPatientWallet(aid, WalletConstant.WALLET_PERSON);
			patientBalanceSubWallet = patientWalletDao.queryPatientBalanceSubWalletByAid(aid);
		}
		JSONArray recordsarr = patient.getJSONArray("records");
		Iterator<Object> pit = recordsarr.iterator();
		while (pit.hasNext()) {
			JSONObject record = (JSONObject) pit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String tradeNo = record.getString("trade_no");
			String trxNo = record.containsKey("trx_no") ? record.getString("trx_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			String uid = record.containsKey("uid") ? record.getString("uid") : null;
			String channelId = record.containsKey("channel_id") ? record.getString("channel_id") : null;
			int changeDirection = record.getInteger("change_direction");
			int type = record.getInteger("type");

			// 处理，添加患者账户流水
			if (type == WalletConstant.WALLET_TYPE_BALANCE) {
				// 余额子账户类型
				int changeAmount = record.getInteger("change_amount");
				PatientBalanceSubWalletRecords pbw = new PatientBalanceSubWalletRecords();
				pbw.setTradeNo(tradeNo);
				pbw.setTrxNo(trxNo);
				pbw.setBizNo(bizNo);
				pbw.setTrxCode(trxCode);
				pbw.setChangeAmount(changeAmount);
				pbw.setChangeDirection(changeDirection);
				pbw.setChannelId(channelId);
				pbw.setUid(uid);
				pbw.setWalletNo(patientBalanceSubWallet.getWalletNo());
				pbw.setAid(aid);
				pbw.setRecordedTime(getTime(time));//时间类型转换
				patientWalletDao.addBalanceRecords(pbw); // 保存患者余额子账户流水
				// 更新用户账户金额
				patientWalletDao.updateBalanceById(patientBalanceSubWallet.getId(),
						changeDirection == 0 ? changeAmount : -changeAmount);
			} else if (type == WalletConstant.WALLET_TYPE_COUPON) {
				// 券子账户
				String couponId = record.containsKey("coupon_id") ? record.getString("coupon_id") : null;
				int couponAmount = record.getInteger("coupon_amount");
				PatientCouponSubWalletRecords pcw = new PatientCouponSubWalletRecords();
				pcw.setTradeNo(tradeNo);
				pcw.setTrxNo(trxNo);
				pcw.setBizNo(bizNo);
				pcw.setCouponAmount(couponAmount);
				pcw.setChangeDirection(changeDirection);
				pcw.setChannelId(channelId);
				pcw.setUid(uid);
				pcw.setCouponId(couponId);
				pcw.setWalletNo(patientBalanceSubWallet.getWalletNo());
				pcw.setAid(aid);
				pcw.setRecordedTime(getTime(time));//时间类型转换
				patientWalletDao.addCouponRecords(pcw);// 保存患者券子账户流水
			} else if (type == WalletConstant.WALLET_TYPE_VIP) {
				// vip子账户
				String serviceId = record.containsKey("service_id") ? record.getString("service_id") : null;
				int serviceAmount = record.getInteger("service_amount");
				PatientVipSubWalletRecords pvw = new PatientVipSubWalletRecords();
				pvw.setTradeNo(tradeNo);
				pvw.setTrxNo(trxNo);
				pvw.setBizNo(bizNo);
				pvw.setServiceAmount(serviceAmount);
				pvw.setChangeDirection(changeDirection);
				pvw.setChannelId(channelId);
				pvw.setUid(uid);
				pvw.setServiceId(serviceId);
				pvw.setWalletNo(patientBalanceSubWallet.getWalletNo());
				pvw.setAid(aid);
				pvw.setRecordedTime(getTime(time));//时间类型转换
				patientWalletDao.addVipRecords(pvw);// 保存患者vip子账户流水
			} else if (type == WalletConstant.WALLET_TYPE_ACTIVITY) {
				// 活动子账户
				String activityId = record.containsKey("activity_id") ? record.getString("activity_id") : null;
				int activityAmount = record.getInteger("activity_amount");
				PatientActivitySubWalletRecords paw = new PatientActivitySubWalletRecords();
				paw.setTradeNo(tradeNo);
				paw.setTrxNo(trxNo);
				paw.setBizNo(bizNo);
				paw.setActivityAmount(activityAmount);
				paw.setChangeDirection(changeDirection);
				paw.setChannelId(channelId);
				paw.setUid(uid);
				paw.setActivityId(activityId);
				paw.setWalletNo(patientBalanceSubWallet.getWalletNo());
				paw.setAid(aid);
				paw.setRecordedTime(getTime(time));//时间类型转换
				patientWalletDao.addActivityRecords(paw);// 保存患者活动子账户流水
			} else {
				throw new WalletBizException(WalletBizException.ERROR, "账户类型不存在");
			}
		}
		// 平台内容
		Iterator<Object> fit = platform.iterator();
		while (fit.hasNext()) {
			JSONObject record = (JSONObject) fit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String tradeNo = record.getString("trade_no");
			String trxNo = record.containsKey("trx_no") ? record.getString("trx_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int type = record.getInteger("type");
			int changeAmount = record.getInteger("change_amount");

			PlatformWalletRecords pfw = new PlatformWalletRecords();
			pfw.setTradeNo(tradeNo);
			pfw.setTrxNo(trxNo);
			pfw.setBizNo(bizNo);
			pfw.setTrxCode(trxCode);
			pfw.setChangeAmount(changeAmount);
			pfw.setChangeDirection(changeDirection);
			pfw.setType(type);
			pfw.setWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			pfw.setRecordedTime(getTime(time));//时间类型转换
			if (type == WalletConstant.WALLET_TYPE_COUPON) {
				// 余额子账户类型
				String couponId = record.containsKey("coupon_id") ? record.getString("coupon_id") : null;
				pfw.setCouponId(couponId);
			} else if (type == WalletConstant.WALLET_TYPE_VIP) {
				// vip子账户
				String serviceId = record.containsKey("service_id") ? record.getString("service_id") : null;
				pfw.setVipServiceId(serviceId);
			} else if (type == WalletConstant.WALLET_TYPE_ACTIVITY) {
				// 活动子账户
				String activityId = record.containsKey("activity_id") ? record.getString("activity_id") : null;
				pfw.setActivityId(activityId);
			}
			platformWalletDao.addPlatformRecords(pfw);// 保存平台账户流水
			// 更新平台账户金额
			if (type == WalletConstant.WALLET_TYPE_BALANCE) {
				PlatformWallet platformWallet = platformWalletDao.queryIdByWalletNo(WalletConstant.PLATFORM_WALLET_NO);
				platformWalletDao.updateBalanceById(platformWallet.getId(),
						changeDirection == 0 ? changeAmount : -changeAmount);
			}
		}
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}

	@Override
	public void WalletRecordRefundSuccess(String require, String token) throws WalletBizException, Exception {
		this.WalletRecordPaySuccess(require, token);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordRisk(String require, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(require)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数require");
		}
		JSONObject json = JSONObject.parseObject(require);
		JSONObject doctor = json.getJSONObject("doctor");
		JSONArray platform = json.getJSONArray("platform");
		String msgId = json.getString("msgId");
		long time = 0l;
		if(null != json.getLong("biz_time")){
			time = json.getLong("biz_time");
		}
		// 判断消息是否已经消费
		MqRecords mqRecords = mqRecordsDao.getMqRecord(Integer.valueOf(msgId));
		if (mqRecords != null) {
			int state = mqRecords.getState();
			if (state == 1) {
				throw new WalletBizException(WalletBizException.ERROR, "消息重复发送，丢弃");
			}
		}
		// doctor内容
		String docid = doctor.getString("doc_id");
		DoctorBalanceSubWallet doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		if (doctorBalanceSubWallet == null) {
			// 医生余额子账户不存在，创建账户
			doctorWalletService.createDoctorWallet(docid, WalletConstant.WALLET_PERSON);
			doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		}
		JSONArray recordsarr = doctor.getJSONArray("records");
		Iterator<Object> pit = recordsarr.iterator();
		while (pit.hasNext()) {
			JSONObject record = (JSONObject) pit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int changeAmount = record.getInteger("change_amount");
			String trade_no = record.containsKey("trade_no") ? record.getString("trade_no") : null;
			String biz_type = record.containsKey("biz_type") ? record.getString("biz_type") : null;
			String income_id = record.containsKey("income_id") ? record.getString("income_id") : null;
			String detail_type = record.containsKey("detail_type") ? record.getString("detail_type") : null;
			String detail_id = record.containsKey("detail_id") ? record.getString("detail_id") : null;
			// 处理，添加患者账户流水
			DoctorBalanceSubWalletRecords dbw = new DoctorBalanceSubWalletRecords();
			dbw.setBizNo(bizNo);
			dbw.setTrxCode(trxCode);
			dbw.setChangeAmount(changeAmount);
			dbw.setChangeDirection(changeDirection);
			dbw.setWalletNo(doctorBalanceSubWallet.getWalletNo());
			dbw.setDocid(docid);
			dbw.setTradeNo(trade_no);
			if(StringUtils.isNotEmpty(biz_type)){
				dbw.setBizType(Integer.valueOf(biz_type));
			}
			dbw.setIncomeId(income_id);
			if(StringUtils.isNotEmpty(detail_type)){
				dbw.setDetailType(Integer.valueOf(detail_type));
			}
			dbw.setDetailId(detail_id);
			dbw.setRecordedTime(getTime(time));
			
			doctorWalletDao.addBalanceRecords(dbw); // 保存医生余额子账户流水
			// 更新医生账户金额
			doctorWalletDao.updateBalanceById(doctorBalanceSubWallet.getId(),
					changeDirection == 0 ? changeAmount : -changeAmount);
		}
		// 平台内容
		Iterator<Object> fit = platform.iterator();
		while (fit.hasNext()) {
			JSONObject record = (JSONObject) fit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String tradeNo = record.getString("trade_no");
			String trxNo = record.containsKey("trx_no") ? record.getString("trx_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int type = record.getInteger("type");
			int changeAmount = record.getInteger("change_amount");

			PlatformWalletRecords pfw = new PlatformWalletRecords();
			pfw.setTradeNo(tradeNo);
			pfw.setTrxNo(trxNo);
			pfw.setBizNo(bizNo);
			pfw.setTrxCode(trxCode);
			pfw.setChangeAmount(changeAmount);
			pfw.setChangeDirection(changeDirection);
			pfw.setType(type);
			pfw.setWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			pfw.setRecordedTime(getTime(time));//时间类型转换
			platformWalletDao.addPlatformRecords(pfw);// 保存平台账户流水
			// 更新平台账户金额
			PlatformWallet platformWallet = platformWalletDao.queryIdByWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			platformWalletDao.updateBalanceById(platformWallet.getId(),
					changeDirection == 0 ? changeAmount : -changeAmount);
		}
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordFrozen(String require, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(require)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数require");
		}
		JSONObject json = JSONObject.parseObject(require);
		JSONObject doctor = json.getJSONObject("doctor");
		JSONArray platform = json.getJSONArray("platform");
		String msgId = json.getString("msgId");
		long time = 0l;
		if(null != json.getLong("biz_time")){
			time = json.getLong("biz_time");
		}
		// 判断消息是否已经消费
		MqRecords mqRecords = mqRecordsDao.getMqRecord(Integer.valueOf(msgId));
		if (mqRecords != null) {
			int state = mqRecords.getState();
			if (state == 1) {
				throw new WalletBizException(WalletBizException.ERROR, "消息重复发送，丢弃");
			}
		}
		// doctor内容
		String docid = doctor.getString("doc_id");
		DoctorBalanceSubWallet doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		if (doctorBalanceSubWallet == null) {
			// 医生余额子账户不存在，创建账户
			doctorWalletService.createDoctorWallet(docid, WalletConstant.WALLET_PERSON);
			doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		}
		JSONArray recordsarr = doctor.getJSONArray("records");
		Iterator<Object> pit = recordsarr.iterator();
		while (pit.hasNext()) {
			JSONObject record = (JSONObject) pit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int changeAmount = record.getInteger("change_amount");
//			long occurTime = record.getLongValue("occurTime");
//			Long date = record.getLongValue("date");
			// 处理，添加医生账户流水
			DoctorBalanceSubWalletRecords dbw = new DoctorBalanceSubWalletRecords();
			dbw.setBizNo(bizNo);
			dbw.setTrxCode(trxCode);
			dbw.setChangeAmount(changeAmount);
			dbw.setChangeDirection(changeDirection);
			dbw.setWalletNo(doctorBalanceSubWallet.getWalletNo());
			dbw.setDocid(docid);
//			dbw.setRecordedTime(new Timestamp(occurTime*1000));
//			if(date != null){
//				dbw.setRecordedTime(new Timestamp(date*1000));
//			}
			dbw.setRecordedTime(getTime(time));
			doctorWalletDao.addBalanceRecords(dbw); // 保存医生余额子账户流水
			if(WalletConstant.DOCTOR_RECORD_TYPE_IN.equals(trxCode)){
				// 更新医生账户金额
				doctorWalletDao.updateBalanceById(doctorBalanceSubWallet.getId(), changeDirection == 0 ? changeAmount : -changeAmount);
			}
			if(WalletConstant.DOCTOR_RECORD_TYPE_FROZEN.equals(trxCode)){
				// 更新医生账户冻结金额
				doctorWalletDao.updateFrozenBalanceById(doctorBalanceSubWallet.getId(), changeDirection == 0 ? changeAmount : -changeAmount);
			}
		}
		// 平台内容
		Iterator<Object> fit = platform.iterator();
		while (fit.hasNext()) {
			JSONObject record = (JSONObject) fit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String tradeNo = record.getString("trade_no");
			String trxNo = record.containsKey("trx_no") ? record.getString("trx_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int type = record.getInteger("type");
			int changeAmount = record.getInteger("change_amount");
	
			PlatformWalletRecords pfw = new PlatformWalletRecords();
			pfw.setTradeNo(tradeNo);
			pfw.setTrxNo(trxNo);
			pfw.setBizNo(bizNo);
			pfw.setTrxCode(trxCode);
			pfw.setChangeAmount(changeAmount);
			pfw.setChangeDirection(changeDirection);
			pfw.setType(type);
			pfw.setWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			pfw.setRecordedTime(getTime(time));//时间类型转换
			platformWalletDao.addPlatformRecords(pfw);// 保存平台账户流水
			// 更新平台账户金额
			PlatformWallet platformWallet = platformWalletDao.queryIdByWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			platformWalletDao.updateBalanceById(platformWallet.getId(),changeDirection == 0 ? changeAmount : -changeAmount);
		}
		// 消息消费
			mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
		}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordTransactionDone(String require, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(require)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数require");
		}
		JSONObject json = JSONObject.parseObject(require);
		JSONObject doctor = json.getJSONObject("doctor");
		JSONArray platform = json.getJSONArray("platform");
		String msgId = json.getString("msgId");
		long time = 0l;
		if(null != json.getLong("biz_time")){
			time = json.getLong("biz_time");
		}
		// 判断消息是否已经消费
		MqRecords mqRecords = mqRecordsDao.getMqRecord(Integer.valueOf(msgId));
		if (mqRecords != null) {
			int state = mqRecords.getState();
			if (state == 1) {
				throw new WalletBizException(WalletBizException.ERROR, "消息重复发送，丢弃");
			}
		}
		// doctor内容
		String docid = doctor.getString("doc_id");
		DoctorBalanceSubWallet doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		if (doctorBalanceSubWallet == null) {
			// 医生余额子账户不存在，创建账户
			doctorWalletService.createDoctorWallet(docid, WalletConstant.WALLET_PERSON);
			doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		}
		JSONArray recordsarr = doctor.getJSONArray("records");
		Iterator<Object> pit = recordsarr.iterator();
		while (pit.hasNext()) {
			JSONObject record = (JSONObject) pit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int changeAmount = record.getInteger("change_amount");
	
			String trade_no = record.containsKey("trade_no") ? record.getString("trade_no") : null;
			String biz_type = record.containsKey("biz_type") ? record.getString("biz_type") : null;
			String income_id = record.containsKey("income_id") ? record.getString("income_id") : null;
			String detail_type = record.containsKey("detail_type") ? record.getString("detail_type") : null;
			String detail_id = record.containsKey("detail_id") ? record.getString("detail_id") : null;
			// 处理，添加医生账户流水
			DoctorBalanceSubWalletRecords dbw = new DoctorBalanceSubWalletRecords();
			dbw.setBizNo(bizNo);
			dbw.setTrxCode(trxCode);
			dbw.setChangeAmount(changeAmount);
			dbw.setChangeDirection(changeDirection);
			dbw.setWalletNo(doctorBalanceSubWallet.getWalletNo());
			dbw.setDocid(docid);
			dbw.setTradeNo(trade_no);
			if(StringUtils.isNotEmpty(biz_type)){
				dbw.setBizType(Integer.valueOf(biz_type));
			}
			dbw.setIncomeId(income_id);
			if(StringUtils.isNotEmpty(detail_type)){
				dbw.setDetailType(Integer.valueOf(detail_type));
			}
			dbw.setDetailId(detail_id);
			dbw.setRecordedTime(getTime(time));//时间类型转换
			doctorWalletDao.addBalanceRecords(dbw); // 保存医生余额子账户流水
			// 更新医生账户金额
			doctorWalletDao.updateBalanceById(doctorBalanceSubWallet.getId(),
					changeDirection == 0 ? changeAmount : -changeAmount);
		}
		// 平台内容
		Iterator<Object> fit = platform.iterator();
		while (fit.hasNext()) {
			JSONObject record = (JSONObject) fit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String tradeNo = record.getString("trade_no");
			String trxNo = record.containsKey("trx_no") ? record.getString("trx_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int type = record.getInteger("type");
			int changeAmount = record.getInteger("change_amount");
	
			PlatformWalletRecords pfw = new PlatformWalletRecords();
			pfw.setTradeNo(tradeNo);
			pfw.setTrxNo(trxNo);
			pfw.setBizNo(bizNo);
			pfw.setTrxCode(trxCode);
			pfw.setChangeAmount(changeAmount);
			pfw.setChangeDirection(changeDirection);
			pfw.setType(type);
			pfw.setWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			pfw.setRecordedTime(getTime(time));//时间类型转换
			platformWalletDao.addPlatformRecords(pfw);// 保存平台账户流水
			// 更新平台账户金额
			PlatformWallet platformWallet = platformWalletDao.queryIdByWalletNo(WalletConstant.PLATFORM_WALLET_NO);
			platformWalletDao.updateBalanceById(platformWallet.getId(),
					changeDirection == 0 ? changeAmount : -changeAmount);
		}
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordDkSuccess(String require, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(require)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数require");
		}
		JSONObject json = JSONObject.parseObject(require);
		JSONObject doctor = json.getJSONObject("doctor");
		String msgId = json.getString("msgId");
		long time = 0l;
		if(null != json.getLong("biz_time")){
			time = json.getLong("biz_time");
		}
		// 判断消息是否已经消费
		MqRecords mqRecords = mqRecordsDao.getMqRecord(Integer.valueOf(msgId));
		if (mqRecords != null) {
			int state = mqRecords.getState();
			if (state == 1) {
				throw new WalletBizException(WalletBizException.ERROR, "消息重复发送，丢弃");
			}
		}
		// doctor内容
		String docid = doctor.getString("doc_id");
		DoctorBalanceSubWallet doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		if (doctorBalanceSubWallet == null) {
			// 医生余额子账户不存在，创建账户
			doctorWalletService.createDoctorWallet(docid, WalletConstant.WALLET_PERSON);
			doctorBalanceSubWallet = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
		}
		JSONArray recordsarr = doctor.getJSONArray("records");
		Iterator<Object> pit = recordsarr.iterator();
		while (pit.hasNext()) {
			JSONObject record = (JSONObject) pit.next();
			String bizNo = record.containsKey("biz_no") ? record.getString("biz_no") : null;
			String trxCode = record.containsKey("trx_code") ? record.getString("trx_code") : null;
			int changeDirection = record.getInteger("change_direction");
			int changeAmount = record.getInteger("change_amount");
		
			// 处理，添加患者账户流水
			DoctorBalanceSubWalletRecords dbw = new DoctorBalanceSubWalletRecords();
			dbw.setBizNo(bizNo);;
			dbw.setTrxCode(trxCode);
			dbw.setChangeAmount(changeAmount);
			dbw.setChangeDirection(changeDirection);
			dbw.setWalletNo(doctorBalanceSubWallet.getWalletNo());
			dbw.setDocid(docid);
			dbw.setRecordedTime(getTime(time));//时间类型转换
			doctorWalletDao.addBalanceRecords(dbw); // 保存医生余额子账户流水
			// 更新医生账户金额
			doctorWalletDao.updateBalanceById(doctorBalanceSubWallet.getId(),changeDirection == 0 ? changeAmount : -changeAmount);
			// 更新可提现金额=0 提现中金额=0
			DoctorBalanceSubWallet dbs = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docid);
			dbs.setWithdrawalBalanceCan(0);
			dbs.setWithdrawalBalanceWait(0);
			doctorWalletDao.updateDoctorBalanceSubWallet(dbs);
		}
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}
	/**
	 * 将long类型的时间进行转换
	 * @param time
	 * @return
	 */
	public Timestamp getTime(Long time){
		if( 0l!=time){
			if(time.toString().length()<=10){
				time = time*1000;
			}
			Timestamp ts = new Timestamp(time);
			return ts;
		}
		return null;
	}
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletSetWithdrawalCan(int balance, String docId, String msgId,String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(docId)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数docId");
		}
		DoctorBalanceSubWallet dbs = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docId);
		dbs.setWithdrawalBalanceCan(balance);
		dbs.setWithdrawalBalanceWait(0);
		doctorWalletDao.updateDoctorBalanceSubWallet(dbs);
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletSetWithdrawalIng(int balance, String docId, String msgId, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(docId)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数docId");
		}
		DoctorBalanceSubWallet dbs = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docId);
		dbs.setWithdrawalBalanceCan(0);
		dbs.setWithdrawalBalanceWait(balance);
		doctorWalletDao.updateDoctorBalanceSubWallet(dbs);
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void WalletRecordDkFailure(String bizNo, int balance, String docId, String msgId, String token) throws WalletBizException, Exception {
		if (StringUtils.isEmpty(docId)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数docId");
		}
		DoctorBalanceSubWallet dbs = doctorWalletDao.queryDoctorBalanceSubWalletByDocid(docId);
		dbs.setWithdrawalBalanceCan(balance);
		dbs.setWithdrawalBalanceWait(0);
		doctorWalletDao.updateDoctorBalanceSubWallet(dbs);
		// 消息消费
		mqRecordsDao.updateState(Integer.valueOf(msgId), MqConstant.MQ_STATUS_USED);
	}
}
